package org.snail.common.dingding;

import com.dingtalk.open.client.api.model.corp.*;
import org.snail.common.dingding.department.DepartmentHelper;
import org.snail.common.dingding.exception.OApiException;
import org.snail.common.dingding.auth.AuthHelper;
import org.snail.common.dingding.chat.ChatHelper;
import org.snail.common.dingding.media.MediaHelper;
import org.snail.common.dingding.user.UserHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 本地测试方法钉钉API
 */
public class Demo {

//    public static void main(String[] args) throws Exception {
//
//        TreeNode treeNode = DepartmentService.getDepartmentTreeFromDingDing("25393462", "BU9", "L2");
//        List<String> managerList = new ArrayList<String>();
//        outNode(treeNode, "", 0, managerList);
//    }
//
//    private static void outNode(TreeNode treeNode, String parentName, int i, List<String> managerList) throws  Exception{
//
//        for (int j = 0; j <= i; j++) {
//            System.out.print("----");
//        }
//
//        System.out.println("【佰钧成" + parentName + "_" + treeNode.getName() + "】 " +  treeNode.getLv());
//        long offset = 0;
//        int size = 50;
//
////        CorpUserDetailList corpUserDetailsList = UserHelper.getUserDetails(AuthHelper.getAccessToken(), Long.valueOf(treeNode.getId())
////                , offset, size, null);;
////        List<CorpUserDetail> corpUserList2 = corpUserDetailsList.getUserlist();
////
////        if(null!=managerList &&  managerList.size()>0) {
////            for (String oneManager: managerList) {
////                System.out.println("-------------------------------------------oneManager name:" + oneManager + ", " + "");
////            }
////            managerList.clear();
////        }
////
////        for (CorpUserDetail corpUser: corpUserList2) {
////            System.out.println("-------------------------------------------corpUser name:" + corpUser.getName() + ", " + corpUser.getUserid());
////        }
////
////
//        List<TreeNode> list = treeNode.getChildren();
//        if(null!=list && list.size()>0) {
//            List<String> currentManagerList = new ArrayList<String>();
//            for (TreeNode oneTreeNode: list) {
//                currentManagerList = oneTreeNode.getManager();
//                System.out.println("-------------------------------------------oneTreeNode.getManager() name:" + oneTreeNode.getManager() + ", " + "");
//                outNode(oneTreeNode,treeNode.getName(), i+1, currentManagerList);
//            }
//        }
//
//        test2();
//    }

    public static void test() throws Exception{
        List<Department> departments = new ArrayList<Department>();
        departments = DepartmentHelper.listDepartments(AuthHelper.getAccessToken(), "25393462");
        System.out.println("depart num:" + departments.size());
        for (int i = 0; i < departments.size(); i++) {
            Department department = departments.get(i);
            if("交付管理部".equals(department.getName())) {
                System.out.println("---------depart name:" + department.getName());
                List<String> useridlist  = new ArrayList<String>();

                long offset = 0;
                int size = 50;

                CorpUserList corpUserList = new CorpUserList();
                corpUserList = UserHelper.getDepartmentUser(AuthHelper.getAccessToken(), Long.valueOf(departments.get(i).getId())
                        , offset, size, null);

                List<CorpUser>  corpUserList1 = corpUserList.getUserlist();
                for (CorpUser corpUser: corpUserList1) {
                    useridlist.add(corpUser.getUserid());
                    useridlist.add(corpUser.getUserid());
                    System.out.println("corpUser name:" + corpUser.getName() + ", " + corpUser.getUserid());
                }

                List<String> dellist  = new ArrayList<String>();
                dellist.add("ceshi");
                String chatid = ChatHelper.createChat(AuthHelper.getAccessToken(),department.getName(),"066217045824030227",  useridlist);
                System.out.println("chatid:" + chatid);

                ChatHelper.updateChat(AuthHelper.getAccessToken(), "chat76c0b79ca19a15df72ad0dd7a5b5ee43", department.getName(), "066217045824030227", useridlist, dellist);

                MessageBody.TextBody textBody = new MessageBody.TextBody();
                textBody.setContent("测试");
                ChatHelper.sendMessageToChat(AuthHelper.getAccessToken(), "chat76c0b79ca19a15df72ad0dd7a5b5ee43", "066217045824030227", MessageType.TEXT, textBody);
            }
        }
    }

    public static void test2() throws Exception{
        try {
            List<Department> departments = new ArrayList<Department>();
            departments = DepartmentHelper.listDepartments(AuthHelper.getAccessToken(), "25393462");
            System.out.println("depart num:" + departments.size());
            for (int i = 0; i < departments.size(); i++) {
                Department department = departments.get(i);
//                System.out.println("depart :"+ department.getId() + ", " + department.getName() +  ", " + department.getParentid());
                if ("运营部".equals(department.getName())) {
                    List<String> useridlist  = new ArrayList<String>();

                    long offset = 0;
                    int size = 50;

//                    CorpUserList corpUserList = new CorpUserList();
//                    corpUserList = UserHelper.getDepartmentUser(AuthHelper.getAccessToken(), Long.valueOf(departments.get(i).getId())
//                            , offset, size, null);
                    CorpUserDetailList corpUserDetailsList = UserHelper.getUserDetails(AuthHelper.getAccessToken(), Long.valueOf(departments.get(i).getId())
                            , offset, size, null);

//                    List<CorpUser>  corpUserList1 = corpUserList.getUserlist();
                    List<CorpUserDetail> corpUserList2 = corpUserDetailsList.getUserlist();
                    for (CorpUserDetail corpUser: corpUserList2) {
                        useridlist.add(corpUser.getUserid());
                        useridlist.add(corpUser.getUserid());
                        System.out.println("-----------corpUser name:" + corpUser.getName() + ", " + corpUser.getUserid() + corpUser);
                    }
                    useridlist.remove("0509395438788633");
                    useridlist.remove("0509395438788633");

                    List<String> dellist  = new ArrayList<String>();
                    dellist.add("0509395438788633");
//                  String chatid = ChatHelper.createChat(AuthHelper.getAccessToken(),department.getName(),"066217045824030227",  useridlist);
                    System.out.println("useridlist:" + useridlist);

                    ChatHelper.updateChat(AuthHelper.getAccessToken(), "chat76c0b79ca19a15df72ad0dd7a5b5ee43", department.getName(), "066217045824030227", useridlist, dellist);

                    UploadResult result = MediaHelper.upload(AuthHelper.getAccessToken(), MediaType.FILE, new File("D:\\0-WorkDoc\\0-BU9\\20180108-钉钉考勤指南.pdf"));
                    String mediaId = result.getMedia_id();
                    System.out.println("-----------mediaId:" + mediaId);

                    UploadResult result2 = MediaHelper.upload(AuthHelper.getAccessToken(), MediaType.IMAGE, new File("C:\\Users\\WJ\\Pictures\\menu.saveimg.savepath20180524125517.jpg"));
                    String mediaId2 = result2.getMedia_id();
                    System.out.println("-----------mediaId2:" + mediaId2);

                    ChatInfo chatinfo = ChatHelper.getChatInfo(AuthHelper.getAccessToken(), "chat76c0b79ca19a15df72ad0dd7a5b5ee43");

                    MessageBody.FileBody fileBody = new MessageBody.FileBody();
                    fileBody.setMedia_id(mediaId);

                    MessageBody.ImageBody imgBody = new MessageBody.ImageBody();
                    imgBody.setMedia_id(mediaId2);

//                  MessageBody.TextBody textBody = new MessageBody.TextBody();
//                  textBody.setContent("好消息好消息，温州皮革厂要倒闭了");
//                  ChatHelper.sendMessageToChat(AuthHelper.getAccessToken(), "chat76c0b79ca19a15df72ad0dd7a5b5ee43", "066217045824030227", MessageType.FILE, fileBody);

                    ChatHelper.sendMessageToChat(AuthHelper.getAccessToken(), "chat76c0b79ca19a15df72ad0dd7a5b5ee43", "066217045824030227", MessageType.IMAGE, imgBody);

//                    for (String userID : useridlist) {
//                        UserMessageHelper.sendMessageToUser(AuthHelper.getAccessToken(), userID, "",Env.AGENT_ID,  MessageType.TEXT, textBody);
//                    }
                }
            }

//            MessageBody.TextBody textBody = new MessageBody.TextBody();
//            textBody.setContent("测试222");
//            UserMessageHelper.sendMessageToUser(AuthHelper.getAccessToken(), "066217045824030227", "",Env.AGENT_ID,  MessageType.TEXT, textBody);
        } catch (OApiException e) {
            e.printStackTrace();
        }
    }
}
